<%@ page import="cn.kgc.util.PageSupport" %>
<%@ page import="cn.kgc.pojo.NewsDetail" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.kgc.pojo.Category" %>
<%@ page contentType="text/html;charset=utf-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String path = request.getContextPath();  // /web01
    String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    //  http://localhost:8888/web01/   相对路径
%>
<html>
<head>
    <base href="<%=basepath%>"/>
    <title>Title</title>

</head>
<body>
<%
    PageSupport  pageSupport = (PageSupport) request.getAttribute("page");
    String title= (String) request.getAttribute("title");
    String categoryId= (String) request.getAttribute("categoryId");
    if(title==null){
        title="";
    }
    Integer id=0;
    if(categoryId!=null){
       id=Integer.parseInt(categoryId);
    }

    List<NewsDetail> list=pageSupport.getData();
    List<Category> categoryList= (List<Category>) request.getAttribute("categorylist");
    //新闻列表
    request.setAttribute("newslist",list);
    //分类列表
    request.setAttribute("catelist",categoryList);
%>


<div class="main-content-right">
    <!--即时新闻-->
    <div class="main-text-box">
        <div class="main-text-box-tbg">
            <div class="main-text-box-bbg">
                <form name="searchForm" id="searchForm" action="news?type=list" method="get">
                    <div>
                        新闻分类：
                        <select name="categoryId">
                            <%
                                StringBuffer str=new StringBuffer();
                                if(id==0){
                                    str.append("<option value='0'>全部</option>");
                                }else{
                                    str.append("<option value='0' selected>全部</option>");
                                }
                                for (int i=1;i<=categoryList.size();i++){
                                        if(i==id){
                                            String s="<option value='"+i+"' selected>"+categoryList.get(i-1).getName()+"</option>";
                                            str.append(s);
                                        }else{
                                            String s="<option value='"+i+"'>"+categoryList.get(i-1).getName()+"</option>";
                                            str.append(s);
                                        }
                                }
                                out.print(str.toString());
                            %>
                        </select>
                        新闻标题<input type="text" name="title" id="title" value="<%=title%>">
                        <input type="hidden" name="type" value="list"/>
                        <input type="hidden" name="pageNo" value="1"/>
                        <button type="submit" class="page-btn">GO</button>
                        <button type="button" onclick="addNews();" class="page-btn">增加</button>

                        <input type="hidden" name="pageSize" value="10">
                        <input type="hidden" name="totalPageCount" value="2">
                    </div>
                </form>
                <table cellpadding="1" cellspacing="1" class="admin-list">
                    <thead>
                    <tr class="admin-list-head">
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                <c:forEach var="news" items="${newslist}" varStatus="index">
                <tr <c:if test="${index.index%2!=0}">class="admin-list-td-h2"</c:if> >
                        <td><a href='news?type=view&&id=${news.id}'>${news.title}</a></td>
                        <td>${news.author}</td>
                        <td>${news.createDate}</td>
                        <td><a href='adminNewsCreate.jsp?id=${news.id}'>修改</a>
                            <a href="javascript:if(confirm('确认是否删除此新闻？')) location='news?type=delete&id=${news.id}'">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                    </tbody>
                </table>
                <div class="page-bar">
                    <ul class="page-num-ul clearfix">
                        <li>${page.totalcount}条记录&nbsp;&nbsp; ${page.pageNo}/${page.totalPage}页</li>
                        <a href="javascript:page_nav(document.forms[0],1)">首页</a>
                        <a href="javascript:page_nav(document.forms[0],${(page.pageNo-1)<1?1:(page.pageNo-1)})">上一页</a>&nbsp;&nbsp;
                        <a href="javascript:page_nav(document.forms[0],${(page.pageNo+1)<(page.totalPage)?(page.pageNo+1):(page.totalPage)})">下一页</a>
                        <a href="javascript:page_nav(document.forms[0],${page.totalPage})">最后一页</a>&nbsp;&nbsp;
                    </ul>
                    <span class="page-go-form"><label>跳转至</label>
	               <input type="text" name="inputPage" id="inputPage" class="page-key">页
	     <button type="button" class="page-btn" onclick="">GO</button>
		</span>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script>
    function page_nav(frm,num){
        frm.pageNo.value=num;
        frm.submit();
    }

    function addNews(){
        location.href="day04/createnews.jsp";
    }
</script>
